﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class RegistrarProtag : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    String SQL, mssg;
    AdmiDatos.Service ad = new AdmiDatos.Service();
    System.Data.DataSet dst = new System.Data.DataSet();
    Formato1 vt = new Formato1();

    private void habilitarExiste()
    {
        BtnAgregar.Enabled = false;
        BtnEliminar.Enabled = true;
    }
    private void habilitarNuevo()
    {
        BtnAgregar.Enabled = true;
        BtnEliminar.Enabled = false;

    }
    private void deshabilitarBotones()
    {
        BtnAgregar.Enabled = false;
        BtnEliminar.Enabled = false;
        vt.LimpiartextBoxes(this);
    }

    protected void BtnAgregar_Click(object sender, EventArgs e)
    {
        try
        {
            SQL = "Insert into Protagonistas(ProtagonistaId,Nombre,Apellido,Nacionalidad,Sexo)";
            SQL += " Values('" + TxtIdProtag.Text + "','" + TxtNombreP.Text + "','" + TxtApellidoP.Text + "','" + TxtNacion.Text + "','" + DropDownList1.SelectedIndex.ToString() + "')";

            ad.actualizaD(SQL, ref mssg);
            vt.MessageBox("Los Registros han sido ingresado exitosamente!", this);
            deshabilitarBotones();
        }
        catch (Exception ex)
        {
            vt.MessageBox(ex.Message, this);
        }
    }
    
    protected void BtnEliminar_Click(object sender, EventArgs e)
    {
         try
        {
            SQL = "Delete from Protagonistas Where ProtagonistaId = '" + TxtIdProtag.Text + "'";

            ad.actualizaD(SQL, ref mssg);
            vt.MessageBox("Los Registros se han eliminado exitosamente!", this);
            deshabilitarBotones();
        }
         catch (Exception ex)
         {
             vt.MessageBox(ex.Message, this);
         }

    }
    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
       
        try
        {
             SQL = "Select * From Protagonistas Where ProtagonistaId = '" + TxtIdProtag.Text + "'";
             ad.ObtenerD(ref mssg, SQL, ref dst);
             if (dst.Tables[0].Rows.Count > 0)
             {
                 TxtNombreP.Text = dst.Tables[0].Rows[0][1].ToString();
                 TxtApellidoP.Text = dst.Tables[0].Rows[0][2].ToString();
                 TxtNacion.Text = dst.Tables[0].Rows[0][3].ToString();
                 habilitarExiste();
             }
             else
            {
                vt.MessageBox("Registro no encontrado!", this);
                habilitarNuevo();
                Session["IdProtag"] = TxtIdProtag.Text;
                vt.LimpiartextBoxes(this);
                TxtIdProtag.Text = Session["IdProtag"].ToString();
            } 
        } 
        catch (Exception ex)
        {
            vt.MessageBox(ex.Message, this);
        } 
                 
    
    }
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        bool sexo;

    }
}